/**
 * Created by sjg on 17/4/4.
 */
var CONFIG = require('../../../../common/config');
var CODE = require('../../../../common/code');
var async = require('async');
var express = require('express');
var router = express.Router();
var checkUtil = require('../../util/checkUtil');
var printUtil = require('../../util/printUtil');
var ItbItemsModel = require('../../../../common/mysql/mapping/ItbItemsModel');
//------------------------------------------------------------------
/**
 * 获取商品列表
 *
 * 参数: device	"0 表示从pc来的请求
 * 1 标识一键生成iOS端
 * 2 标识一键生成android端
 * 3 标识一键生成 app 嵌入网页
 * 4 标识一键生成 web 端
 * 5 标识一键生成微信端
 * 101 表示pc美化大师/102表示pc wps/103表示wps android/104表示wps ios
 * 201 标识利楚商务
 * 301 标识酒单网
 *
 * 参数: typeM  商品二级分类, 40100 名片, 40200 传单, 40300 展架, 40400 酒标
 * 参数: typeS  商品三级分类,
 *              -1 标记全部, 标识获取当前二级分类下所有商品, 默认为-1
 *              若传递对应三级分类, 则标识获取该三级分类下所有商品
 *
 * 参数: pageNum  请求页
 * 参数: pageSize 请求页大小
 * 参数[可选]: withTotalPage  标记是否获取总页数,
 *              0 标记为不获取总页数, 默认为0
 *              1 标记为获取总页数"
 *
 * 原接口: /print/getPrintList
 * 测试地址:  http://localhost:3000/tp/v1/goods/getGoodsList
 * */
function getGoodsList(req, res, next){

    //var obj = {"list":[{"id":134,"title":"红色线型简约名片","thumb":"http:\\/\\/itbour-back.oss-cn-hangzhou.aliyuncs.com\\/image\\/U13396\\/2017\\/03\\/31\\/183833696_rc2TC20b8j40kUT4X3ap.jpg","price":"印刷 20.00元\\/2盒"},{"id":110,"title":"黑白简约个性名片","thumb":"http:\\/\\/itbour-back.oss-cn-hangzhou.aliyuncs.com\\/image\\/U13396\\/2017\\/03\\/31\\/103705279_7mdU8nhK3JjrwU32LHek.jpg","price":"印刷 20.00元\\/2盒"}],"currentPage":1,"totalPage":1};
    //return printUtil.ret_func_json(1, '成功', obj, res);

    var device = checkUtil.stringFromReq(req, 'device');
    if(!device) {
        return printUtil.ret_func_json(0,CODE.DEVICE_EMPTY,{},res);
    }

    var typeM = checkUtil.numberFromReq(req, 'typeM');
    if(!typeM) {
        return printUtil.ret_func_json(0,CODE.TYPE_M_EMPTY,{},res);
    }
    var typeS = checkUtil.numberFromReq(req, 'typeS') || -1;

    var withTotalPage = checkUtil.numberFromReq(req, 'withTotalPage');

    var pageNum = checkUtil.numberFromReq(req, 'pageNum') || 0;
    var pageSize = checkUtil.numberFromReq(req, 'pageSize') || 12;
    printUtil.getPrintListEx(typeM, typeS, pageNum, pageSize, function(error, list){
        if(error){
            console.log(error);
            return printUtil.ret_func_json(0, CODE.OPERATION_FAILED,{},res);
        }

        if(withTotalPage>0) {
            printUtil.getPrintListExCnt(typeM, typeS, function(err, retCnt){
                if(err){
                    console.log(err);
                    return printUtil.ret_func_json(0, CODE.OPERATION_FAILED,{},res);
                }

                var pgCnt = 1;
                if(pageSize>0) {
                    pgCnt = Math.floor((retCnt-1)/pageSize)+1;
                }
                return printUtil.ret_func_json(1, CODE.OPERATION_SUCCESS, {'list':list, currentPage:pageNum, totalPage:pgCnt}, res);
            });

        } else {
            return printUtil.ret_func_json(1, CODE.OPERATION_SUCCESS, {'list':list},res);
        }
    });
}
router.get('/getGoodsList', getGoodsList);
router.post('/getGoodsList', getGoodsList);

//------------------------------------------------------------------
/**
 * 获取商品预览
 * 参数: device	"0 表示从pc来的请求
 * 1 标识一键生成iOS端
 * 2 标识一键生成android端
 * 3  标识一键生成 app 嵌入网页
 * 4 标识一键生成 web 端
 * 5 标识一键生成微信端
 *
 * 101 表示pc美化大师/102表示pc wps/103表示wps android/104表示wps ios
 * 201 标识利楚商务
 * 301 标识酒单网"
 * 参数: id	商品 id
 * 原接口: /print/getPrintPriview
 * 测试地址:  http://localhost:3000/tp/v1/goods/getGoodsList
 * */
function getGoodsPreview(req, res, next) {
    //var obj = {"id":99,"designId":704,"thumbs":["http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U13396/2017/03/28/200644351_6eyZDb9XDwLQg7Qage8g.jpg"],"printPara":[{"title":"180x80cm 亮膜写真","desc":"加厚PP膜 覆亮光膜","size":{"width":80,"height":180,"unit":"cm","desc":"80x180"},"counts":[{"show":"1","minCount":1,"maxCount":1,"price":3000,"unit":"张"},{"show":"2","minCount":2,"maxCount":2,"price":6000,"unit":"张"}]},{"title":"180x80cm普通X型展架","desc":"普通X型展架","size":{"width":80,"height":180,"unit":"cm","desc":"80x180"},"counts":[{"show":"1","minCount":1,"maxCount":1,"price":3800,"unit":"个"},{"show":"2","minCount":2,"maxCount":2,"price":7600,"unit":"个"}]}]};
    //return printUtil.ret_func_json(1, '成功', obj, res);

    var device = checkUtil.stringFromReq(req, 'device');
    if(!device) {
        return printUtil.ret_func_json(0,CODE.DEVICE_EMPTY,{},res);
    }
    var id = checkUtil.numberFromReq(req, 'id');
    if(!id) {
        return printUtil.ret_func_json(0,CODE.GOODS_ID_EMPTY,{},res);
    }

    printUtil.getItemAccessoryItemsId_v5_2(id, function(error, accessoryItemsId){
        var ids = id;
        if(accessoryItemsId) {

            var accIds = accessoryItemsId.toString().split(',');
            accIds = accIds.join(',');
            ids = ids + ',' + accIds;
        }
        // 串行获取数据
        async.series([
                function(callback){
                    // 获取印品信息和用户信息
                    printUtil.get_printItemAndUserInfo(id, function(error,data){ callback(error,data)});
                },
                function(callback){
                    // 获取印刷参数列表
                    //printUtil.get_printPara(id, function(error,data){ callback(error,data)});
                    printUtil.get_itemsPrintParam_v5_2(ids, function(error,data){callback(error,data)});
                }
            ],

            function(err, results){
                if(err){
                    console.log(err);
                    return printUtil.ret_func_json(0,'error',{},res);
                }

                var data = {
                    "id":               id,
                    "designId":         results[0].designId,
                    "thumbs":           results[0].thumbs,
                    "designer":         results[0].designer,
                    "printPara":        results[1]
                };
                return printUtil.ret_func_json(1,CODE.OPERATION_SUCCESS, data,res);
            });
    });
}
router.get('/getGoodsPreview', getGoodsPreview);
router.post('/getGoodsPreview', getGoodsPreview);

//------------------------------------------------------------------
/**
 * 获取配件预览图信息
 * 参数: device	"0 表示从pc来的请求
 * 1 标识一键生成iOS端
 * 2 标识一键生成android端
 * 3  标识一键生成 app 嵌入网页
 * 4 标识一键生成 web 端
 * 5 标识一键生成微信端
 *
 * 101 表示pc美化大师/102表示pc wps/103表示wps android/104表示wps ios
 * 201 标识利楚商务
 * 301 标识酒单网"
 * 参数: id	配件 id
 * 测试地址: localhost:3000/tp/v1/goods/getAccessoryPreview?id=104&device=301
 * */
function getAccessoryPreview(req, res, next) {
    var device = checkUtil.stringFromReq(req, 'device');
    if(!device) {
        return printUtil.ret_func_json(0,CODE.DEVICE_EMPTY,{},res);
    }
    var id = checkUtil.numberFromReq(req, 'id');
    if(!id) {
        return printUtil.ret_func_json(0,CODE.GOODS_ID_EMPTY,{},res);
    }

    var accesstory = new ItbItemsModel({
        item_id : id,
        del_flg: 0
    });

    accesstory.findFromDB(function(error, items){

        if(error) {
            console.log("/tp/v1/goods.js [getAccessoryPreview] error: " + error);
            return printUtil.ret_func_json(0, CODE.OPERATION_FAILED, {}, res);
        }
        var data = {
            id : id,
            thumbs : []
        };
        if(items && items.length > 0) {
            data.thumbs = JSON.parse(items[0].thumbs);
        }
        return printUtil.ret_func_json(1, CODE.OPERATION_SUCCESS, data, res);
    });
}
router.get('/getAccessoryPreview', getAccessoryPreview);
router.post('/getAccessoryPreview', getAccessoryPreview);


//------------------------------------------------------------------



//------------------------------------------------------------------
module.exports = router;